package db.domain;

import db.ObjectJsonConverter;
import db.entitylistener.UserEntityListener;
import lombok.Data;
import org.springframework.transaction.annotation.Propagation;
import sf.core.DBField;
import sf.core.DBObject;
import sf.database.annotations.Comment;
import sf.database.annotations.CreatedDate;
import sf.database.annotations.LastModifiedDate;
import sf.database.annotations.SavedDefaultValue;

import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Data
@EntityListeners({UserEntityListener.class})
@Table(name = "t_persion")
public class PersonJPA extends DBObject {
    @Id
    @GeneratedValue
    private String id;

    @Column
    private Sex sex;

    @Comment("事务级别")
    @Column
    @Enumerated(EnumType.ORDINAL)
    private Propagation propagation;

    @GeneratedValue
    @Column
    private String name;

    @Column(name = "nick_name")
    private String nickName;
    @Column
    private int age;

    @Comment("头像")
    @Lob
    @Column
    private byte[] icon;

    @Lob
    @Column
    private String description;

    @Column(precision = 10, scale = 5)
    @SavedDefaultValue
    private BigDecimal weight = new BigDecimal(0);

    @Column
    @Version
    private String v1;

    @Column
    @Version
    private int v2;

    @Column
    @Temporal(TemporalType.TIMESTAMP)
    @Version
    private Date v3;

    @Lob
    @Column
    @Convert(converter = ObjectJsonConverter.class)
    private Map<String, String> attributes;

    @Comment("创建时间")
    @CreatedDate
    @Column
    @Temporal(TemporalType.TIMESTAMP)
    protected Date created;

    @Comment("修改时间")
    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    protected Date modified;

    @Column
    @Temporal(TemporalType.DATE)
    protected Date d2;

    @Column
    @Temporal(TemporalType.TIME)
    protected Date d3;

    @Transient
    private boolean showMe;

    public enum Field implements DBField {
        id, sex, name, nickName, age, icon,description, weight, v1, v2, v3, attributes, created, modified, d2, d3, propagation
    }
}